package com.example.materialviewpagerdemo;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.widget.TextView;
import android.widget.Toast;

//import com.example.materialviewpagerdemo.bean.DataModel;
//import com.example.materialviewpagerdemo.Presenter.CurrentDataController2;
//import com.example.materialviewpagerdemo.bean.DataModel;
import com.example.materialviewpagerdemo.Presenter.CurrentDataControllern;
import com.example.materialviewpagerdemo.bean.NanxiaoData;
import com.example.materialviewpagerdemo.bean.StaticData;
import com.example.materialviewpagerdemo.util.ChartUtil;
import com.example.materialviewpagerdemo.util.LoadingUtil;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.zyao89.view.zloading.ZLoadingDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ChartActivityn extends AppCompatActivity {
    LoadingUtil loadingUtil;
    Typeface typeface;
    @BindView(R.id.lineChart)
    LineChart lineChart;
    List<Entry> list = new ArrayList<>();
    List<BarEntry> barlist = new ArrayList<>();

    ZLoadingDialog zLoadingDialog;
    @BindView(R.id.chart_pm_one)
    BarChart chartPmOne;


    @BindView(R.id.chart_name)
    TextView chartName;
    @BindView(R.id.chart_currentdata)
    TextView chartCurrentdata;
    @BindView(R.id.unit)
    TextView unit;
    @BindView(R.id.lineChart_name)
    TextView lineChartName;
    @BindView(R.id.barchart_name)
    TextView barchartName;
    @BindView(R.id.card_view_barchart)
    CardView cardViewBarchart;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chart);
        ButterKnife.bind(this);
        zLoadingDialog = new ZLoadingDialog(this);
        loadingUtil = new LoadingUtil(zLoadingDialog);
        loadingUtil.showDialog();
        String flag = (String) getIntent().getExtras().get("flag");
        if ("1".equals(flag)) {
            setTextData("1");
            getDataList(7, "1");
        } else if ("2".equals(flag)) {
            setTextData("2");
            getDataList(7, "2");
        } else if ("3".equals(flag)) {
            setTextData("3");
            getDataList(7, "3");
        } else if ("4".equals(flag)) {
            setTextData("4");
            getDataList(7, "4");
        }

        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    private void getDataList(int number, String flag) {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(StaticData.url)
                .build();
        CurrentDataControllern currentDataController = retrofit.create(CurrentDataControllern.class);
        Call<List<NanxiaoData>> call = currentDataController.query(number, Integer.parseInt(flag));
        call.enqueue(new Callback<List<NanxiaoData>>() {
            @Override
            public void onResponse(Call<List<NanxiaoData>> call, Response<List<NanxiaoData>> response) {
                if (response.body() != null) {
                    ListIterator<NanxiaoData> listIterator = null;
                    try {
                        listIterator = response.body().listIterator();
                    } catch (NullPointerException e) {
                        return;
                    }
                    int i = 0;
                    while (listIterator.hasNext()) {
                        i++;
                        NanxiaoData currentData = listIterator.next();
                        addDataList(currentData, i);
                    }

                    Collections.reverse(list);
                    Collections.reverse(barlist);
                    ChartUtil.setChartData(lineChart, list);
                    ChartUtil.setBarChartData(chartPmOne, barlist);
                    loadingUtil.cancleDialog();
                }


            }

            @Override
            public void onFailure(Call<List<NanxiaoData>> call, Throwable t) {
                loadingUtil.cancleDialog();
                System.out.println("失败");
            }
        });
    }

    public void addDataList(NanxiaoData currentData, int i) {
        Entry entry = null;
        BarEntry barEntry = null;
        i = 10 - i;
        switch ((int) getIntent().getExtras().get("id")) {

            case 1:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getAirtemp1()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getAirtemp1()), ChartUtil.praseData(currentData.getDatetime()));
                break;
            case 2:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getAirhumi1()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getAirhumi1()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 3:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getAirtemp2()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getAirhumi2()), ChartUtil.praseData(currentData.getDatetime()));

                break;

            case 4:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getAirhumi2()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getAirhumi2()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 5:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getAirtemp3()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getAirtemp3()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 6:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getAirhumi3()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getAirhumi3()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 7:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getAirtemp4()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getAirtemp4()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 8:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getAirhumi4()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getAirhumi4()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 9:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilhumi1()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilhumi1()), ChartUtil.praseData(currentData.getDatetime()));

                break;

            case 10:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilelc1()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilelc1()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 11:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoiltemp1()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoiltemp1()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 12:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilhumi2()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilhumi2()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 13:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilelc2()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilelc2()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 14:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoiltemp2()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoiltemp2()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 15:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilhumi3()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilhumi3()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 16:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilelc3()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilelc3()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 17:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoiltemp3()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoiltemp3()), ChartUtil.praseData(currentData.getDatetime()));

                break;

        }
        list.add(entry);
        barlist.add(barEntry);

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setTextData(String flag) {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(StaticData.url)
                .build();
        CurrentDataControllern currentDataController = retrofit.create(CurrentDataControllern.class);
        Call<NanxiaoData> call = currentDataController.queryOne(Integer.parseInt(flag));
        call.enqueue(new Callback<NanxiaoData>() {
            @Override
            public void onResponse(Call<NanxiaoData> call, Response<NanxiaoData> response) {
                try {
                    setData(response);
                } catch (NullPointerException e) {
                    Toast.makeText(ChartActivityn.this, "数据获取失败,请检查网络状态", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(Call<NanxiaoData> call, Throwable t) {

            }
        });


    }


    public void setData(Response<NanxiaoData> response) {
        switch ((int) getIntent().getExtras().get("id")) {
            case 0:
                break;
            case 1:
                unit.setText("℃");
                setRepeatName("空气温度1");
                chartCurrentdata.setText(response.body().getAirtemp1());
                break;
            case 2:
                unit.setText("%");
                setRepeatName("空气湿度1");
                chartCurrentdata.setText(response.body().getAirhumi1());
                break;
            case 3:
                unit.setText("℃");
                setRepeatName("空气温度2");
                chartCurrentdata.setText(response.body().getAirtemp2());
                break;

            case 4:
                unit.setText("%");
                setRepeatName("空气湿度2");
                chartCurrentdata.setText(response.body().getAirhumi2());
                break;
            case 5:
                unit.setText("℃");
                setRepeatName("空气温度3");
                chartCurrentdata.setText(response.body().getAirtemp3());
                break;

            case 6:
                unit.setText("%");
                setRepeatName("空气湿度3");
                chartCurrentdata.setText(response.body().getAirhumi3());
                break;
            case 7:
                unit.setText("℃");
                setRepeatName("空气温度4");
                chartCurrentdata.setText(response.body().getAirtemp4());
                break;
            case 8:
                unit.setText("%");
                setRepeatName("空气湿度4");
                chartCurrentdata.setText(response.body().getAirhumi4());
                break;
            case 9:
                unit.setText("%");
                setRepeatName("土壤湿度1");
                chartCurrentdata.setText(response.body().getSoilhumi1());
                break;

            case 10:
                unit.setText("µs/m");
                setRepeatName("土壤导电率1");
                chartCurrentdata.setText(response.body().getSoilelc1());
                break;

            case 11:
                unit.setText("℃");
                setRepeatName("土壤温度1");
                chartCurrentdata.setText(response.body().getSoiltemp1());
                break;


            case 12:
                unit.setText("%");
                setRepeatName("土壤湿度2");
                chartCurrentdata.setText(response.body().getSoilhumi2());
                break;


            case 13:
                unit.setText("µs/m");
                setRepeatName("土壤导电率2");
                chartCurrentdata.setText(response.body().getSoilelc2());
                break;


            case 14:
                unit.setText("℃");
                setRepeatName("土壤温度2");
                chartCurrentdata.setText(response.body().getSoiltemp2());
                break;
            case 15:
                unit.setText("%");
                setRepeatName("土壤湿度3");
                chartCurrentdata.setText(response.body().getSoilhumi3());
                break;


            case 16:
                unit.setText("µs/m");
                setRepeatName("土壤导电率3");
                chartCurrentdata.setText(response.body().getSoilelc3());
                break;


            case 17:
                unit.setText("℃");
                setRepeatName("土壤温度3");
                chartCurrentdata.setText(response.body().getSoiltemp3());
                break;
        }


    }

    public void setRepeatName(String name) {
        chartName.setText("实时" + name);
        lineChartName.setText(name + "-折线图");
        barchartName.setText(name + "-柱状图");
    }
}
